package org.teachertools.web.jsonview;

import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractCachingViewResolver;

public class JsonViewResolver extends AbstractCachingViewResolver implements Ordered {

    private static final int DEFAULT_ORDER = 1;

    private static final Logger logger = Logger.getLogger(JsonViewResolver.class);

    private JsonView jsonView = new JsonView();

    private int order = DEFAULT_ORDER;

    @Override
    protected View loadView(String viewName, Locale locale) throws Exception {
        boolean isJsonView = "json".equals(viewName);
        if (logger.isDebugEnabled()) {
            logger.debug("Determining if this is a json view, viewName: " + viewName + ", isJsonView: "
                    + isJsonView);
        }
        if (isJsonView) {
            return this.jsonView;
        }
        return null;
    }

    public void setOrder(int newOrder) {
        this.order = newOrder;
    }

    @Override
    public int getOrder() {
        return this.order;
    }

    // @SuppressWarnings("unchecked")
    // @Override
    // public ModelAndView resolveModelAndView(Method handlerMethod, Class handlerType, Object returnValue,
    // ExtendedModelMap implicitModel, NativeWebRequest webRequest) {
    // if (logger.isDebugEnabled()) {
    // logger.debug("Determining if this is a json view");
    // logger.debug("Is annotated by @JsonReponse: " + handlerMethod.isAnnotationPresent(JsonResponse.class));
    // logger.debug("Return Value: : " + returnValue);
    // }
    // if (handlerMethod.isAnnotationPresent(JsonResponse.class)) {
    // return new ModelAndView(this.jsonView, implicitModel);
    // }
    // return UNRESOLVED;
    // }
}
